"""
本代码由[Tkinter布局助手]生成
官网:https://www.pytk.net
QQ交流群:905019785
在线反馈:https://support.qq.com/product/618914
"""
import random
from tkinter import *
from tkinter.ttk import *

class WinGUI(Tk):
    def __init__(self):
        super().__init__()
        self.__win()
        self.__create_widgets()
        
    def __win(self):
        self.title("用户信息收集")
        width = 600
        height = 500
        screenwidth = self.winfo_screenwidth()
        screenheight = self.winfo_screenheight()
        geometry = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
        self.geometry(geometry)
        self.resizable(width=False, height=False)
        
    def __create_widgets(self):
        # 名字标签和输入框
        Label(self, text="名字", anchor="center").place(x=45, y=37, width=50, height=30)
        self.name_entry = Entry(self)
        self.name_entry.place(x=125, y=36, width=150, height=30)
        
        # 手机号标签和输入框
        Label(self, text="手机号", anchor="center").place(x=40, y=109, width=50, height=30)
        self.phone_entry = Entry(self)
        self.phone_entry.place(x=125, y=110, width=150, height=30)
        
        # 性别标签和按钮
        Label(self, text="性别", anchor="center").place(x=39, y=188, width=50, height=30)
        
        # 使用IntVar跟踪性别选择
        self.gender_var = IntVar(value=0)
        Radiobutton(self, text="女", variable=self.gender_var, value=0).place(x=126, y=189, width=50, height=30)
        Radiobutton(self, text="男", variable=self.gender_var, value=1).place(x=213, y=189, width=50, height=30)
        
        # 手机品牌选择
        Label(self, text="手机品牌", anchor="center").place(x=39, y=246, width=80, height=30)
        self.phone_brand = Combobox(self, state="readonly")
        self.phone_brand['values'] = ("苹果", "华为", "小米", "三星", "OPPO", "vivo", "其他")
        self.phone_brand.current(0)  # 设置默认选择
        self.phone_brand.place(x=126, y=246, width=150, height=30)
        
        # 提交按钮
        self.submit_btn = Button(self, text="提交信息", command=self.submit_data)
        self.submit_btn.place(x=200, y=320, width=100, height=40)
        
        # 结果显示区域
        self.result_text = Text(self, state="disabled")
        self.result_text.place(x=50, y=380, width=500, height=100)
    
    def submit_data(self):
        """收集并显示用户信息"""
        name = self.name_entry.get()
        phone = self.phone_entry.get()
        gender = "女" if self.gender_var.get() == 0 else "男"
        brand = self.phone_brand.get()
        
        if not name or not phone:
            self.show_result("错误：请填写姓名和手机号")
            return
        
        if not phone.isdigit() or len(phone) != 11:
            self.show_result("错误：手机号应为11位数字")
            return
        
        result = f"姓名: {name}\n手机号: {phone}\n性别: {gender}\n手机品牌: {brand}"
        self.show_result("信息提交成功:\n" + result)
    
    def show_result(self, message):
        """在结果区域显示消息"""
        self.result_text.config(state="normal")
        self.result_text.delete(1.0, END)
        self.result_text.insert(END, message)
        self.result_text.config(state="disabled")

if __name__ == "__main__":
    app = WinGUI()
    app.mainloop()